<?php


namespace App\Http\Controllers\Mp;


use App\Model\Device;
use App\Utils\Result;
use Illuminate\Http\Request;
use Endroid\QrCode\ErrorCorrectionLevel;
use Endroid\QrCode\LabelAlignment;
use Endroid\QrCode\QrCode;

class QrCodeController
{
    public function deviceQrCode(Request $request, $id)
    {
        $device = Device::query()->find($id);

        if($device->qrcode && $device->qrcode != '[]') {
            return Result::success(['url' => $device->qrcode]);
        }

        $qrCode = new QrCode(env('APP_URL') . '/deviceDetail?sn=' . $device->sn);
        $qrCode->setSize(300);
        $qrCode->setMargin(10);

// Set advanced options
        $qrCode->setWriterByName('png');
        $qrCode->setEncoding('UTF-8');
        $qrCode->setErrorCorrectionLevel(ErrorCorrectionLevel::HIGH());
        $qrCode->setForegroundColor(['r' => 0, 'g' => 0, 'b' => 0, 'a' => 0]);
        $qrCode->setBackgroundColor(['r' => 255, 'g' => 255, 'b' => 255, 'a' => 0]);
        //$qrCode->setLabel('Scan the code', 16, __DIR__.'/../assets/fonts/noto_sans.otf', LabelAlignment::CENTER());
        //$qrCode->setLogoPath(__DIR__.'/../assets/images/symfony.png');
        //$qrCode->setLogoSize(150, 200);
        //$qrCode->setValidateResult(false);

// Round block sizes to improve readability and make the blocks sharper in pixel based outputs (like png).
// There are three approaches:
        $qrCode->setRoundBlockSize(true, QrCode::ROUND_BLOCK_SIZE_MODE_MARGIN); // The size of the qr code is shrinked, if necessary, but the size of the final image remains unchanged due to additional margin being added (default)
        $qrCode->setRoundBlockSize(true, QrCode::ROUND_BLOCK_SIZE_MODE_ENLARGE); // The size of the qr code and the final image is enlarged, if necessary
        $qrCode->setRoundBlockSize(true, QrCode::ROUND_BLOCK_SIZE_MODE_SHRINK); // The size of the qr code and the final image is shrinked, if necessary

// Set additional writer options (SvgWriter example)
        $qrCode->setWriterOptions(['exclude_xml_declaration' => true]);

// Directly output the QR code
        //header('Content-Type: '.$qrCode->getContentType());
        //echo $qrCode->writeString();

// Save it to a file
        $qrCode->writeFile(public_path('/storage/qrcode/qrcode-' . $device->sn . '.png'));

// Generate a data URI to include image data inline (i.e. inside an <img> tag)
        //$dataUri = $qrCode->writeDataUri();

        $device->qrcode = env('APP_URL') . '/storage/qrcode/qrcode-' . $device->sn . '.png';
        $device->save();

        return Result::success(['url' => $device->qrcode]);
    }
}
